<?php

class Domain_Application {

    public function rule_config() {

        return [
            "CocaCola_Vip.Birthday"     => "可乐会员生日查询",
            "CocaCola_Vip.Credit"       => "可乐会员可乐币查询",
            "CocaCola_Vip.CreditAdd"    => "可乐会员可乐币增加",
            "CocaCola_Vip.CreditDel"    => "可乐会员可乐币扣除",
            "CocaCola_Vip.CreditUpdate" => "可乐会员可乐币设置",
            "CocaCola_Vip.No"           => "可乐会员编号查询",
            "CocaCola_Vip.State"        => "可乐会员注册状态",
            "CocaCola_Vip.Username"     => "可乐会员用户名查询",
            "Service.Id"     => "身份证查询",
            "Service.Ip"     => "IP地址归属地查询",
            "Service.Aqi"     => "空气质量查询",
            "Service.Weather"     => "天气查询",
            "Service.Phone"     => "手机号码查询",
            "Service.Robot"     => "聊天机器人",
            "Service.HistoryDay"     => "历史上的事件",
            "Service.Shorturl"     => "短网址生成",
            "Service.SendMail"     => "发送邮件",
        ];

    }

    public function checkAdmin($username) {
        $model = new Model_Application();
        $rs = $model->getByUserName($username);
        return $rs;

    }

    public function getAdminById($userid) {
        $model = new Model_Application();
        $rs = $model->getByUserId($userid);
        return $rs;

    }

    public function updatelastlogintime($username) {
        $model = new Model_Application();
        $rs = $model->updatelastlogintime($username);
        return $rs;

    }

    public function getlist() {
        $model = new Model_Application();

        $rs = $model->getlist();
        return $rs;

    }
    public function info($id) {
        $model = new Model_Application();
        $rs = $model->getById($id);
        $rs['rules']=@json_decode($rs['rules']);
        return $rs;

    }

    public function resetSecretKey($id){
        $helper= new Common_Helper();
        $SECRETKEY =$helper->createRandStr(32);
        $data = [
            "secretkey"      => $SECRETKEY,
        ];
        $model = new Model_Application();
        if($model->update($data, $id)) {
            return $data["secretkey"];
        };
        return NULL;
    }

    public function add($rules) {
        $helper= new Common_Helper();
        $APPID =$helper->createRandStr(14,"SP");
        $SECRETKEY =$helper->createRandStr(32);

        $data = [
            "appid"      => $APPID,
            "secretkey"      => $SECRETKEY,
            "rules"    => $rules
        ];
        $model = new Model_Application();
        return $model->add($data);
    }

    public function update($rules,$id) {
        $data = [
            "rules"    => $rules
        ];
        $model = new Model_Application();
        return $model->update($data, $id);
    }

    public function delete($id) {
        if (!$id) {
            return false;
        }
        $model = new Model_Application();
        return $model->deleteById($id);

    }
}
